package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import object.TimeSheet;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;

public class TS_Update extends JDialog implements ActionListener{
	
	String date;
	String id;
	String fullName;
	JLabel lblDate;
	JLabel lblEmId;
	JLabel lblEmName;
	JLabel lblMorning;
	JCheckBox checkBoxMor;
	JLabel lblAfternoon;
	JCheckBox checkBoxAft;
	JLabel lblEvening;
	JCheckBox checkBoxEven;
	JButton btnSave;
	JButton btnCancel;
	private JLabel lblDate2;
	private JLabel lblId;
	private JLabel lblName;
	
	public TS_Update(JFrame main, String day, String emId, String emFullName){
		super(main, "Sửa Bảng Chấm Công", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		date = day;
		id = emId;
		fullName = emFullName;
	    int height = 210;
	    int width = 420;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
	    setResizable(false);
	    initComponet();
	}
	
	public void clear(){
		setVisible(false);
		getRootPane().removeAll();
		removeAll();
		dispose();
	}
	
	public void initComponet(){
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblDate = new JLabel("Ngày:");
		lblDate.setFont(UtilityGUI.fontHeader2);
		GridBagConstraints gbc_lblDate = new GridBagConstraints();
		gbc_lblDate.anchor = GridBagConstraints.WEST;
		gbc_lblDate.insets = new Insets(10, 10, 8, 5);
		gbc_lblDate.gridx = 0;
		gbc_lblDate.gridy = 0;
		getContentPane().add(lblDate, gbc_lblDate);
		
		lblDate2 = new JLabel(date);
		lblDate2.setFont(UtilityGUI.fontHeader2);
		lblDate2.setForeground(UtilityGUI.colorHeader1);
		GridBagConstraints gbc_lblDate2 = new GridBagConstraints();
		gbc_lblDate2.anchor = GridBagConstraints.WEST;
		gbc_lblDate2.insets = new Insets(10, 0, 8, 2);
		gbc_lblDate2.gridx = 1;
		gbc_lblDate2.gridy = 0;
		getContentPane().add(lblDate2, gbc_lblDate2);
		
		lblEmId = new JLabel("Mã NV:");
		lblEmId.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblEmId = new GridBagConstraints();
		gbc_lblEmId.anchor = GridBagConstraints.WEST;
		gbc_lblEmId.insets = new Insets(0, 10, 5, 5);
		gbc_lblEmId.gridx = 0;
		gbc_lblEmId.gridy = 1;
		getContentPane().add(lblEmId, gbc_lblEmId);
		
		lblId = new JLabel(id);
		lblId.setFont(UtilityGUI.fontHeader3);
		lblId.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_lblId = new GridBagConstraints();
		gbc_lblId.anchor = GridBagConstraints.WEST;
		gbc_lblId.insets = new Insets(0, 0, 5, 2);
		gbc_lblId.gridx = 1;
		gbc_lblId.gridy = 1;
		getContentPane().add(lblId, gbc_lblId);
		
		lblEmName = new JLabel("Nhân viên:");
		lblEmName.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblEmName = new GridBagConstraints();
		gbc_lblEmName.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmName.anchor = GridBagConstraints.WEST;
		gbc_lblEmName.gridx = 2;
		gbc_lblEmName.gridy = 1;
		getContentPane().add(lblEmName, gbc_lblEmName);
		
		lblName = new JLabel(fullName);
		lblName.setFont(UtilityGUI.fontHeader3);
		lblName.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.gridwidth = 2;
		gbc_lblName.insets = new Insets(0, 0, 5, 2);
		gbc_lblName.anchor = GridBagConstraints.WEST;
		gbc_lblName.gridx = 3;
		gbc_lblName.gridy = 1;
		getContentPane().add(lblName, gbc_lblName);
		
		lblMorning = new JLabel("Ca Sáng");
		lblMorning.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblMorning = new GridBagConstraints();
		gbc_lblMorning.anchor = GridBagConstraints.WEST;
		gbc_lblMorning.insets = new Insets(3, 0, 5, 2);
		gbc_lblMorning.gridx = 1;
		gbc_lblMorning.gridy = 2;
		getContentPane().add(lblMorning, gbc_lblMorning);
		
		checkBoxMor = new JCheckBox();
		GridBagConstraints gbc_checkBoxMor = new GridBagConstraints();
		gbc_checkBoxMor.fill = GridBagConstraints.BOTH;
		gbc_checkBoxMor.insets = new Insets(3, 0, 5, 5);
		gbc_checkBoxMor.gridx = 2;
		gbc_checkBoxMor.gridy = 2;
		getContentPane().add(checkBoxMor, gbc_checkBoxMor);
		
		lblAfternoon = new JLabel("Ca chiều");
		lblAfternoon.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblAfternoon = new GridBagConstraints();
		gbc_lblAfternoon.anchor = GridBagConstraints.WEST;
		gbc_lblAfternoon.insets = new Insets(0, 0, 5, 2);
		gbc_lblAfternoon.gridx = 1;
		gbc_lblAfternoon.gridy = 3;
		getContentPane().add(lblAfternoon, gbc_lblAfternoon);
		
		checkBoxAft = new JCheckBox();
		GridBagConstraints gbc_checkBoxAfter = new GridBagConstraints();
		gbc_checkBoxAfter.fill = GridBagConstraints.BOTH;
		gbc_checkBoxAfter.insets = new Insets(0, 0, 5, 5);
		gbc_checkBoxAfter.gridx = 2;
		gbc_checkBoxAfter.gridy = 3;
		getContentPane().add(checkBoxAft, gbc_checkBoxAfter);
		
		lblEvening = new JLabel("Ca đêm");
		lblEvening.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEvening = new GridBagConstraints();
		gbc_lblEvening.anchor = GridBagConstraints.WEST;
		gbc_lblEvening.insets = new Insets(0, 0, 5, 2);
		gbc_lblEvening.gridx = 1;
		gbc_lblEvening.gridy = 4;
		getContentPane().add(lblEvening, gbc_lblEvening);
		
		checkBoxEven = new JCheckBox();
		GridBagConstraints gbc_checkBoxEven = new GridBagConstraints();
		gbc_checkBoxEven.fill = GridBagConstraints.BOTH;
		gbc_checkBoxEven.insets = new Insets(0, 0, 5, 5);
		gbc_checkBoxEven.gridx = 2;
		gbc_checkBoxEven.gridy = 4;
		getContentPane().add(checkBoxEven, gbc_checkBoxEven);
		
		btnSave = new JButton("Lưu");
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.anchor = GridBagConstraints.NORTHEAST;
		gbc_btnSave.insets = new Insets(5, 0, 5, 5);
		gbc_btnSave.gridx = 3;
		gbc_btnSave.gridy = 5;
		btnSave.addActionListener(this);
		getContentPane().add(btnSave, gbc_btnSave);
		
		btnCancel = new JButton("Hủy");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.insets = new Insets(5, 0, 5, 2);
		gbc_btnCancel.anchor = GridBagConstraints.NORTHWEST;
		gbc_btnCancel.gridx = 4;
		gbc_btnCancel.gridy = 5;
		btnCancel.addActionListener(this);
		getContentPane().add(btnCancel, gbc_btnCancel);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave){
			TimeSheet ts = new TimeSheet();
			ts.setEmId(id);
			ts.setDate(date);
			ts.setMor(checkBoxMor.isSelected());
			ts.setAft(checkBoxAft.isSelected());
			ts.setEven(checkBoxEven.isSelected());
			boolean flag = Main.actionTS.updateTS(ts, Main.userId);
			if(flag == true){
				this.setVisible(false);
				JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
							UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
				String day = StringUtil.subStr(date, "/").get(0);
				int row = Integer.parseInt(day)-1;
				TS_View.tblEmTs.getModel().setValueAt(checkBoxMor.isSelected(), row, 1);
				TS_View.tblEmTs.getModel().setValueAt(checkBoxAft.isSelected(), row, 2);
				TS_View.tblEmTs.getModel().setValueAt(checkBoxEven.isSelected(), row, 3);
			}else{
				JOptionPane.showMessageDialog(null, UtilityGUI.errorAction,
							UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);	
			}
		}else if(e.getSource() == btnCancel){
			clear();
		}	
	}
}
